﻿@using TamoCRM.Core
@using TamoCRM.Domain.StatusCare
@model TamoCRM.Web.Mvc.Areas.Admin.Models.Contacts.ContactCreateModel

@{
    ViewBag.Title = "Thu hồi Contact";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

@{
    var lstLevels = ViewBag.Levels;
    var lstUserTvts = ViewBag.UserTvts;

    var statusCares = ViewBag.StatusCares;
    ((List<StatusCareInfo>)statusCares).RemoveAll(c => c.Id == 0);
    ((List<StatusCareInfo>)statusCares).Insert(0, new StatusCareInfo { Id = 0, Name = "Tất cả" });
    var lstStatusCares = new SelectList(statusCares, "Id", "Name");

    var statusMaps = ViewBag.StatusMaps;
    var lstStatusMaps = new SelectList(statusMaps, "Id", "Name");
}

<div class="breadcrumbs" id="breadcrumbs">
    <script type="text/javascript">
        try {
            ace.settings.check('breadcrumbs', 'fixed');
        }
        catch (e) {

        }
    </script>

    <ul class="breadcrumb">
        <li>
            <i class="icon-home home-icon"></i>
            <a href="/Home">Trang chủ</a>
        </li>

        <li>
            <a href="javascript:void(0);">Danh mục</a>
        </li>
        <li class="active">Contact</li>
    </ul>
</div>

<div class="page-content">
    <div class="page-header">
        <h1>Thu hồi Contact</h1>
    </div>
    <!-- /.page-header -->

    <div class="row">
        <div class="col-xs-12">
            <!-- PAGE CONTENT BEGINS -->
            @using (Ajax.BeginForm("FilterContactRecovery", "Contact", new AjaxOptions { HttpMethod = "GET" }, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form", id = "filter_contact_recovery_form" }))
            {
                @Html.AntiForgeryToken()
                <div class="form-group">
                    <div class="col-sm-12" id="alert">
                    </div>
                </div>
                <div style="border: 1px solid #c5d0dc; padding: 15px;">
                    <div class="form-group" style="margin-bottom: 0px;">
                        <div class="col-sm-7">
                            <div>
                                <input type="radio" id="chkContactNew" name="contacts" checked="checked" />
                                <label for="chkContactNew" class="bolder">Contact mới</label>
                            </div>
                            <div class="space-4"></div>
                            <div>
                                <input type="radio" id="chkContactOld" name="contacts" />
                                <label for="chkContactOld" class="bolder">Contact có hẹn</label>
                            </div>
                            <div id="divLevels" style="display: none;">
                                @foreach (var item in lstLevels)
                                {
                                    <div class="col-sm-3">
                                        <input type="checkbox" name="chkLevels" id='@(item.LevelId)_chkLevel' />
                                        <label for="@(item.LevelId)_chkLevel">@item.Name</label>
                                    </div>
                                }
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            <div>
                                <div class="col-sm-4" style="padding-left: 0px;">
                                    <label class="bolder">Trạng thái chăm sóc</label>
                                </div>
                                <div class="col-sm-4">
                                    @Html.DropDownList("ddlStatusCare", lstStatusCares, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            <div>
                                <div class="col-sm-4" style="padding-left: 0px;">
                                    <label class="bolder">Trạng thái gọi</label>
                                </div>
                                <div class="col-sm-4">
                                    @Html.DropDownList("ddlStatusMap", lstStatusMaps, new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            <div>
                                <div class="col-sm-5" style="padding-left: 0px;">
                                    <label class="bolder">Không chăm sóc quá (Ngày)</label>
                                </div>
                                <div class="col-sm-3">
                                    @Html.TextBox("ddlDay", "7", "number", new { type = "number", min = "1", max = "10000", @class = "collection", @style="width:100%;" })
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-5">
                            <div>
                                <label class="bolder">Chọn TVTS</label>
                            </div>
                            <table border="1" class="table table-striped table-bordered table-hover" style="margin-bottom: 0px; height: 250px; overflow-y: auto; display: block;">
                                <thead>
                                    <tr>
                                        <td class="col-sm-1" style="font-weight: bold">Nhóm</td>
                                        <td class="col-sm-3" style="font-weight: bold">Họ và tên</td>
                                        <td class="center col-sm-1" style="font-weight: bold">
                                            <input type="checkbox" id="chkUsers" /></td>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in lstUserTvts)
                                    {
                                        <tr>
                                            <td class="col-sm-1">
                                                <label>@item.GroupName</label>
                                            </td>
                                            <td class="col-sm-3">
                                                <label for="@(item.UserID)_chkUser">@item.FullName</label>
                                            </td>
                                            <td class="center col-sm-1">
                                                <input type="checkbox" name='chkUser' id='@(item.UserID)_chkUser' />
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            }

            <div class="space-6"></div>
            <button id="btnSubmitAll" class="btn-primary" style="float: right; margin-right: 10px;">Thu hồi Tất cả</button>
            <button id="btnSubmit" class="btn-primary" style="float: right; margin-right: 10px;">Thu hồi Contact</button>
            <button id="btnGoFilter" class="btn-primary" style="float: right; margin-right: 10px;">Lọc Contact</button>
            <div class="clearfix"></div>

            <div class="space-6"></div>
            <table id="grid-table"></table>
            <div id="grid-pager"></div>


            <!-- PAGE CONTENT ENDS -->
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</div>

<script type="text/javascript">
    var url = 'admin/api/contact/filterrecovery?users={0}&type={1}&levels={2}&statusMapId={3}&statusCareId={4}&day={5}&branchId={6}';
    var grid_selector = "#grid-table";
    var heades = ['Id', 'Họ và tên', 'SĐT', 'Email', 'Trường tốt nghiệp', 'Ngành đăng ký', 'Trạng thái chăm sóc', 'Trạng thái gọi', 'Level', 'Nơi sống', 'Ghi chú', 'Điểm tuyển sinh/CTV gọi', 'Ngày tìm nguồn', 'Trường', 'Ngày phân contact', '<input type="checkbox" id="chkContacts" onclick="fixCheckBoxGrid(event)" />'];
    var model = [
        { name: 'Id', index: 'Id', editable: false, sorttype: 'string', width: '0px' },
        { name: 'Fullname', index: 'Fullname', editable: false, sorttype: 'string', width: '70px' },
        { name: 'Mobile', index: 'Mobile', editable: false, sorttype: 'string', width: '70px' },
        { name: 'Email', index: 'Email', editable: false, sorttype: 'string', width: '70px' },
        { name: 'SchoolName', index: 'SchoolName', editable: false, sorttype: 'string', width: '70px' },
        { name: 'MajorName', index: 'MajorName', editable: false, sorttype: 'string', width: '70px' },
        { name: 'StatusCare', index: 'StatusCare', editable: false, sorttype: 'string', width: '70px' },
        { name: 'StatusMap', index: 'StatusMap', editable: false, sorttype: 'string', width: '70px' },
        { name: 'Level', index: 'Level', editable: false, sorttype: 'string', width: '50px' },
        { name: 'LocationName', index: 'LocationName', editable: false, sorttype: 'string', width: '70px' },
        { name: 'Notes', index: 'Notes', editable: false, sorttype: 'string', width: '100px' },
        { name: 'ChannelName', index: 'ChannelName', editable: false, sorttype: 'string', width: '70px' },
        { name: 'RegisteredDate', index: 'RegisteredDate', editable: false, sorttype: 'string', width: '60px' },
        { name: 'School', index: 'School', editable: false, sorttype: 'string', width: '60px' },
        { name: 'HandoverDate', index: 'HandoverDate', editable: false, sorttype: 'string', width: '60px' },
        { name: 'chkContact', index: 'chkContact', editable: true, edittype: 'checkbox', align: 'center', width: '40px', editoptions: { value: "True:False" }, formatter: checkboxFomatter, formatoptions: { disabled: false } }
    ];

    $(document).ready(function () {
        var branchId = '@ViewBag.BranchId';
        $('#ddlDay').val('7');
        loadStatusMap();
        
        setupGridWithoutEdit("#grid-table", "#grid-pager", url.format('', '@((int)TodayType.New)', '', '', $('#ddlDay').val(), branchId), "admin/api/contact/edit", "Id", "Danh sách Contact", heades, model);
        $("#btnGoFilter").click(function (e) {
            e.preventDefault();

            // Param
            var users = buildParamUsers();
            var levels = buildParamLevels();
            var statusMapId = $('#ddlStatusMap').val();
            var statusCareId = $('#ddlStatusCare').val();
            var day = $('#ddlDay').val(); if (day === undefined || day == 0) day = 0;
            var todayType = $('#chkContactNew').is(':checked') ? '@((int)TodayType.New)' : '@((int)TodayType.Appointment)';

            // Format Url
            var newUrl = '/' + url.format(users, todayType, levels, statusMapId, statusCareId, day, branchId);
            jQuery(grid_selector).jqGrid().setGridParam({ url: newUrl }).trigger("reloadGrid");
        });
        $("#btnSubmit").click(function (e) {
            e.preventDefault();

            var ids = '';
            var items = $("#grid-table td input");
            for (var i = 0; i < items.length; i++) {
                if ($(items[i]).is(':checked')) {
                    ids += ids.length == 0
                        ? $('#grid-table').jqGrid('getCell', i + 1, 'Id')
                        : ',' + $('#grid-table').jqGrid('getCell', i + 1, 'Id');
                }
            }
            if (ids.length == 0) {
                alertError('Bạn chưa chọn contact để thu hồi, vui lòng chọn!');
                return;
            }

            var r = confirm("Có phải bạn muốn thu hồi contact đã chọn?");
            if (r == true) {
                $.ajax({
                    type: 'GET',
                    data: { ids: ids },
                    url: '/admin/api/contact/recovery',
                    success: function(response) {
                        alertSuccess("Thu hồi thành công " + response + " contacts");
                        $("#btnGoFilter").click();
                    },
                    error: function(request, status, ex) {
                        alertError("Hệ thống bị lỗi, thu hồi không thành công.");
                    }
                });
            }
        });
        $("#btnSubmitAll").click(function (e) {
            e.preventDefault();

            // Param
            var users = buildParamUsers();
            var levels = buildParamLevels();
            var statusMapId = $('#ddlStatusMap').val();
            var statusCareId = $('#ddlStatusCare').val();
            var day = $('#ddlDay').val(); if (day === undefined || day == 0) day = 10;
            var todayType = $('#chkContactNew').is(':checked') ? '@((int)TodayType.New)' : '@((int)TodayType.Appointment)';

            var items = $("#grid-table td input");
            if (items.length == 0) {
                alertError('Bạn chưa lọc contact để thu hồi, vui lòng lọc!');
                return;
            }

            var r = confirm("Có phải bạn muốn thu hồi toàn bộ contact?");
            if (r == true) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/api/contact/RecoveryAll',
                    data: {
                        day: day,
                        users: users,
                        levels: levels,
                        branchId: branchId,
                        todayType: todayType,
                        statusMapId: statusMapId,
                        statusCareId: statusCareId
                    },
                    beforeSend: function() {
                        alertProcess();
                    },
                    success: function(response) {
                        alertSuccess("Thu hồi thành công " + response + " contacts");
                        $("#btnGoFilter").click();
                    },
                    error: function(request, status, ex) {
                        alertError("Hệ thống bị lỗi, thu hồi không thành công.");
                    }
                });
            }
        });
        $('#chkContactOld').change(function () {
            var elements = document.getElementsByName('chkLevels');
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).prop('checked', $('#chkContactOld').is(":checked"));
            }
            if ($('#chkContactOld').is(":checked"))
                $('#divLevels').css('display', '');
            else
                $('#divLevels').css('display', 'none');
        });
        $('#chkContactNew').change(function () {
            var elements = document.getElementsByName('chkLevels');
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).prop('checked', $('#chkContactOld').is(":checked"));
            }
            if ($('#chkContactOld').is(":checked"))
                $('#divLevels').css('display', '');
            else
                $('#divLevels').css('display', 'none');
        });
        $("#chkContacts").click(function () {
            checkAll('chkContacts', 'checkbox');
        });
        $("#chkUsers").click(function () {
            checkAll('chkUsers', 'chkUser');
        });

        // StatusCare
        $("#ddlStatusCare").change(function () {
            loadStatusMap();
        });
    });
</script>
